<?php
require_once("../workflow/includes/configure.php");

// Récupération de l'année de renouvellement dans la base
$srv = 'ns302986.ovh.net:3306';
$usr = 'vmagroclient';
$pwd = 'vmagroclient';
$db = 'dev_apel_richcore';

if($_SERVER['HTTP_HOST'] == 'services.apel.fr'){
	$srv = 'ns303892.ovh.net:3306';
	$usr = 'mickael';
	$pwd = 'meuranie';
	$db = 'apel_richcore';
}

$is_connected = mysql_connect($srv, $usr, $pwd);

// Get All Users
$sql = 'SELECT id_user, YEAR(date_inscription) AS year, MONTH(date_inscription) AS month FROM '.$db.'.users';
$result = mysql_query($sql) or die('get all users > ' . mysql_error() . '<br />' . $sql);
$users = array();
$count = 0;
while($return = mysql_fetch_assoc($result)) {
	$users[$count] = $return;
	$count ++;
}

// echo '<pre>';var_dump($users);echo '</pre>';

// Create variables
$year = date('Y', time());
//$month = date('n', time());
$renewall_tab = array();

foreach($users AS $user) {
	/*
	// verification qu'il est concerné par l'intranet
	$user_infos = $SOAP_CLIENT->getUserInformations($_GET['id_user']);
	$to_check = false;
	foreach($user_infos['groups'] AS $group) {
		if($group['id'] == 4 OR $group['id'] == 5) {
			$to_check = true;
		}
	}
	*/

	// Users before september
	if(($user['year'] < ($year - 2)) OR (($user['year']) == $year - 1 AND (int)$user['month'] < 9)) {
		$year_to_inject = $year - 1;
	} else {
		$year_to_inject = $year;
	}
	
	// Inject user in renewall_base
	$sql = 'REPLACE INTO '.$db.'.users_renewall SET id_user = '.$user['id_user'].', year = '.$year_to_inject;
	$result = mysql_query($sql) or die('Inject user in renewall_base > ' . mysql_error() . '<br />' . $sql);
	
	$renewall_tab[$year_to_inject][] = $user['id_user'];
}

echo '<pre>';var_dump($renewall_tab);echo '</pre>';
?>
